<?php


namespace app\api\controller\assistant;

use addons\epay\library\Service;
use app\api\controller\xyb\Base;
use app\api\service\Activity\AssociationJobsService;
use app\api\service\AssistantService;
use app\api\service\UsersService;

use app\common\model\marketing\Association as AssociationModel;
use EasyWeChat\Factory;


class Activity extends Base
{
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /*首页；活动信息*/
    public function user_participation()
    {

        $users = $this->getAuthUsers();

        $user_circle = UsersService::formatUsersCircleStatusInfo($users->id, $users->circle_id);

        $res = AssistantService::retrieveUsersOngoingTasks($users->id, $user_circle['circle_id']);

        $this->success('success', $res);
    }

    /*用户开始激活，生成助力进度表*/
    public function user_registration_participate()
    {
        $users = $this->getAuthUsers();
        if(!$jobs_id = (int)$this->request->post('jobs_id')){
            $this->error('参数错误');
        }
        $user_circle = UsersService::formatUsersCircleStatusInfo($users->id, $users->circle_id);
        $res = AssistantService::userRegistrationParticipate($users->id, $user_circle['circle_id'], $jobs_id);
        if( $res['code'] == 0){
            $this->error($res['msg'], '');
        }
        $this->success($res['msg'], $res['data']);
    }

    /*被邀请人开始助力；显示助力详情*/
    public function invite_people_assist_details()
    {
        if(!$progress_id = $this->request->post('progress_id')){
            $this->error('参数错误');
        }
        $res = AssistantService::invitePeopleAssistDetails($progress_id);
        if( $res['code'] == 0){
            $this->error($res['msg'], '');
        }
        $this->success($res['msg'], $res['data']);
    }

    /*被邀请人开始助力；邀请人*/
    public function invite_people_assist()
    {
        $users = $this->getAuthUsers();
        if(!$progress_id = $this->request->post('progress_id')){
            $this->error('参数错误');
        }
        $res = AssistantService::invitePeopleAssist($users->id, $progress_id);
        if( $res['code'] == 0){
            $this->error($res['msg'], '');
        }
        $this->success($res['msg'], $res['data']);
    }

    public function create_qrcode()
    {
        $progress_id = $this->request->post('progress_id');
        $config = Service::getConfig('wechat');
        $app = Factory::miniProgram([
            "app_id" => $config['miniapp_id'],
            "secret" => $config['miniapp_secret'],
            'response_type' => 'array',
        ]);
        $response = $app->app_code->get('pagesE/invite/receive_handle?progress_id=' . $progress_id, [
            'width' => 100, 'env_version' => 'trial',
        ]);

        $dest_dir = ROOT_PATH . 'public/assistant/';

        $filename = '';
        $url = '';
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs($dest_dir, $progress_id . '.png');
            $url = '/assistant/' . $filename;
        }
        $res = AssistantService::invitePeopleAssistDetails($progress_id);
        $this->success('success', [
            'url'           => cdnurl($url),
            'self_reward'   => isset($res['data']['self_reward'])?$res['data']['self_reward']:[],
            'inviter_reward'=> isset($res['data']['inviter_reward'])?$res['data']['inviter_reward']:[],
            'share_img'     => isset($res['data']['share_img'])?cdnurl($res['data']['share_img']):'',
            'share_title'   => isset($res['data']['share_title'])?$res['data']['share_title']:'',
            'sub_title'     => isset($res['data']['sub_title'])?$res['data']['sub_title']:'',
            'banner'        => isset($res['data']['banner'])?$res['data']['banner']:'',
        ]);
    }

    /*助力列表*/
    public function help_list()
    {
        $users = $this->getAuthUsers();
        $status = $this->request->post('status', 0);
        $res = AssistantService::helpList($users->id, $status);
        $this->success('success', $res);
    }

}